//
//  NewFeatureViewController.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/2/3.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit

private let reuseIdentifier = "reuseIdentifier"

//定义图片数量
private let imageCount = 4

class NewFeatureViewController: UICollectionViewController {

    //storyboard 会自动注册cell并且指定layout
    
    //重写集合视图控制器的构造方法
    /*
        1.让外界调用更加简单  不需要传参数
        2.layout 和视图的大小比较固定
    */
    init() {
        //实例化layout
        let layout = UICollectionViewFlowLayout()
        //1.指定layout 的itemSize  cell在实例化的时候 就会使用这个size
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        //设置分页滚动
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //手动注册cell
        self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
        return cell
    }
    
    //MARK: 停止滚动的协议方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenWidth)
        if page == imageCount - 1 {
            //开始动画效果
            if let cell = collectionView?.visibleCells().last as? NewFeatureCell {
                cell.startAnimation()
            }
        }
    }
}

class NewFeatureCell: UICollectionViewCell {
    //MARK: 显示图片的index
    var index: Int = 0 {
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
        }
    }
    
    //MARK: 按钮的点击事件
    @objc private func start() {
        NSNotificationCenter.defaultCenter().postNotificationName(APPSwitchRootViewController, object: nil)
    }
    
    private func startAnimation() {
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.hidden = false
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }) { (_) -> Void in
                
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加子视图
        contentView.addSubview(iconImage)
        contentView.addSubview(startButton)
        
        //设置约束 edges 表示相对于 四周约束
        iconImage.xmg_Fill(contentView)
        startButton.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
        
        //设置点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
    }
    
    //实例化子视图控件
    private lazy var iconImage: UIImageView = UIImageView()
    //开始按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImage: "new_feature_finish_button", color: nil)
}